/**
 * JZ UnoCSS - CSS生成器模块 (重构版)
 * @version 2.0.0
 * @author jz
 */

import { utils } from '../utils/utils.js';

// 导入验证器和解析器
import { validateCSSValue, generateSafeCSS } from './generator/validators.js';
import { parseBracketSyntax, parseValueWithUnit } from './generator/parsers.js';

// 导入各类CSS规则处理器
import { processSizeRules } from './generator/css-rules/size.js';
import { processSpacingRules } from './generator/css-rules/spacing.js';
import { processColorRules } from './generator/css-rules/colors.js';
import { processTypographyRules } from './generator/css-rules/typography.js';
import { processLayoutRules } from './generator/css-rules/layout.js';
import { processEffectsRules } from './generator/css-rules/effects.js';
import { processTransitionRules } from './generator/css-rules/transitions.js';

// CSS生成器 - 只处理 uvue 文件
export function generateCSS(className, options = {}) {
    const { platform = 'H5', h5Unit = 'rem', remBase = 16, excludeClasses = [], generateConditional = false } = options;
    const fileType = 'uvue'; // 固定为 uvue
    
    // 检查是否在排除列表中
    if (excludeClasses && Array.isArray(excludeClasses)) {
        for (const excludePattern of excludeClasses) {
            if (typeof excludePattern === 'string') {
                // 精确匹配
                if (className === excludePattern) {
                    return null;
                }
            } else if (excludePattern instanceof RegExp) {
                // 正则表达式匹配
                if (excludePattern.test(className)) {
                    return null;
                }
            }
        }
    }
    
    // 检查平台兼容性
    if (!utils.isPlatformSupported(className, platform, fileType)) {
        return null;
    }

    // uvue 文件需要先解码可能被编码的类名
    let decodedClassName = className;
    // 如果类名包含编码字符（L、R、S、O、C、K、D等），先解码
    if (/[LRSKODCMP]/.test(className)) {
        decodedClassName = utils.decodeClassNameFromUvue(className);
    }
    
    // 使用解码后的类名进行处理
    const processClassName = decodedClassName;
    
    // 方括号语法解析 (如: text-[#333], bg-[#ff0000], text-[30rpx], w-[100px])
    // 使用解码后的类名进行匹配
    const bracketMatch = processClassName.match(/^([a-z-]+)-\[([^\]]+)\]$/);
    if (bracketMatch) {
        const [, property, value] = bracketMatch;
        const styles = parseBracketSyntax(property, value, { platform, h5Unit, remBase });
        return generateSafeCSS(styles);
    }

    // 条件编译类名 (如: h5:block, mp-weixin:hidden)
    const conditionalMatch = processClassName.match(/^(h5|mp-weixin|mp-alipay|mp-baidu|mp-toutiao|mp-qq|app-plus|nvue):(.+)$/);
    if (conditionalMatch) {
        const [, targetPlatform, actualClassName] = conditionalMatch;
        const platformMap = {
            'h5': 'H5',
            'mp-weixin': 'MP-WEIXIN',
            'mp-alipay': 'MP-ALIPAY',
            'mp-baidu': 'MP-BAIDU',
            'mp-toutiao': 'MP-TOUTIAO',
            'mp-qq': 'MP-QQ',
            'app-plus': 'APP-PLUS',
            'nvue': 'APP-PLUS-NVUE'
        };
        
        const mappedPlatform = platformMap[targetPlatform];
        if (mappedPlatform !== platform) {
            return null;
        }
        
        return generateCSS(actualClassName, options);
    }

    // 尝试各个规则处理器
    let result = null;
    
    // 完整的options参数
    const fullOptions = { platform, h5Unit, remBase, generateConditional };
    
    // 尺寸相关规则
    result = processSizeRules(processClassName, fullOptions);
    if (result) return result;
    
    // 间距相关规则
    result = processSpacingRules(processClassName, fullOptions);
    if (result) return result;
    
    // 颜色相关规则
    result = processColorRules(processClassName, fullOptions);
    if (result) return result;
    
    // 字体相关规则
    result = processTypographyRules(processClassName, fullOptions);
    if (result) return result;
    
    // 布局相关规则
    result = processLayoutRules(processClassName, { ...fullOptions, fileType });
    if (result) return result;
    
    // 效果相关规则
    result = processEffectsRules(processClassName, fullOptions);
    if (result) return result;
    
    // 过渡动画相关规则
    result = processTransitionRules(processClassName, fullOptions);
    if (result) return result;
    
    return null;
}

// 重新导出验证器和解析器函数（向后兼容）
export { validateCSSValue, generateSafeCSS, parseBracketSyntax, parseValueWithUnit };